package com.letv.so.util;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLUtil {

    public static void parserXml(File inputXml, Map<String, Map<String,List<String>>> rtnkeys) {
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputXml);
            Element config = document.getRootElement();
            for (Iterator<Element> i = config.elementIterator(); i.hasNext();) {
                Element keys = i.next();
                Map<String,List<String>> entrys =new HashMap<String,List<String>>();
                rtnkeys.put(keys.getName(), entrys);
                for (Iterator<Element> j = keys.elementIterator(); j.hasNext();) {
                    Element node = j.next();
                    entrys.put(node.getName(),Arrays.asList(node.getText().split("\\,")));
                }
            }
        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void main(String...strings) {
        String path = XMLUtil.class.getResource("/").getPath();
        System.out.println(path);
        File inputXml  = new File(path+"/config.xml");
        Map<String, Map<String,List<String>>> rtnkeys = new HashMap<String, Map<String,List<String>>>();
        parserXml(inputXml,rtnkeys);
    }

}
